﻿/*
 * Created by Ranorex
 * User: taurus
 * Date: 2018/6/26
 * Time: 8:28
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Drawing;
using System.Threading;
using WinForms = System.Windows.Forms;

using Ranorex;
using Ranorex.Core;
using Ranorex.Core.Testing;

using System.IO;

namespace FSRecTool.CommonFunction
{
	public class CommonFunc
	{

		public static FSRecTool.FSRecToolRepository repo = FSRecTool.FSRecToolRepository.Instance;
		
		public CommonFunc()
		{
			// Do not delete - a parameterless constructor is required!
		}

		#region 返回到解决方案同级目录
		/// <summary>
		/// 时间：2018-6-26
		/// 作者：袁郑
		/// 功能：返回到解决方案同级目录
		/// </summary>
		/// <returns></returns>
		public static string ReturnSlnBaseDirectory()
		{
			string str=System.AppDomain.CurrentDomain.BaseDirectory;
			str = str.Substring(0,str.LastIndexOf("\\"));
			str = str.Substring(0,str.LastIndexOf("\\"));
			str = str.Substring(0,str.LastIndexOf("\\"));
			str = str.Substring(0,str.LastIndexOf("\\"));
			str = str.Substring(0,str.LastIndexOf("\\"));
			//Report.Info(str);
			return str;
		}
		#endregion
		
		#region 给字符串加上时间水印
		/// <summary>
		/// 时间：2018-4-2
		/// 作者：袁郑
		/// 功能：给字符串加上时间水印
		/// </summary>
		/// <param name="StrName">字符串</param>
		/// <returns>返回带有时间水印的字符串</returns>
		public static string GetStrTimeMark(string StrName)
		{
			System.DateTime dt = System.DateTime.Now; //获取当前时间
			string strCurDate = dt.ToString("yyyy-MM-dd HH:mm:ss");
			strCurDate = CommonFunc.StrOperate4Date(strCurDate);
			StrName += strCurDate;
			Report.Info("带有时间水印的字符串=" + StrName+"");
			return StrName;
		}

		#endregion
		
		#region 字符串中剔除不是数字的字符操作
		/// <summary>
		/// 功能：字符串中剔除不是数字的字符操作 2017/10/19 15:43:49 --> 20171019154349
		/// 作者：袁郑
		/// 时间：2017-12-6
		/// </summary>
		/// <param name="strItem">需要处理的字符串</param>
		/// <returns></returns>
		public static string  StrOperate4Date(string strItem)
		{
			StringBuilder sb = new StringBuilder();
			char[] charItem = strItem.ToCharArray();
			foreach(var item in charItem)
			{
				int ascNum = Asc(item.ToString());
				if(ascNum >= 48 && ascNum <= 57)
				{
					sb.Append(Chr(ascNum));
				}
			}
			return sb.ToString();
		}
		
		#region ASCII和字符串的转换
		/// <summary>
		/// 功能：字符转ASCII
		/// 作者：袁郑
		/// 时间：2017-12-6
		/// </summary>
		/// <param name="chrItem">字符</param>
		/// <returns></returns>
		private static int Asc(string chrItem)
		{
			if(chrItem.Length == 1)
			{
				System.Text.ASCIIEncoding asciiEncoding = new System.Text.ASCIIEncoding();
				int intAsciiCode = (int)asciiEncoding.GetBytes(chrItem)[0];
				return (intAsciiCode);
			}
			else
			{
				throw new Exception("Character is not valid.");
			}
		}
		
		/// <summary>
		/// 功能：ASCII转字符串
		/// 作者：袁郑
		/// 时间；2017-12-6
		/// </summary>
		/// <param name="asciiCode">ascii码</param>
		/// <returns></returns>
		private static string Chr(int asciiCode)
		{
			if (asciiCode >= 0 && asciiCode <= 255)
			{
				System.Text.ASCIIEncoding asciiEncoding = new System.Text.ASCIIEncoding();
				byte[] byteArray = new byte[] { (byte)asciiCode };
				string strCharacter = asciiEncoding.GetString(byteArray);
				return (strCharacter);
			}
			else
			{
				throw new Exception("ASCII Code is not valid.");
			}
		}
		#endregion
		#endregion
		
		#region 保存文件
		/// <summary>
		/// 时间：2018-1-2
		/// 作者：袁郑
		/// 功能：保存文件
		/// </summary>
		/// <param name="path">文件路径</param>
		public static void SaveFilePath(string path)
		{
			Report.Info("=====保存【路径】="+path+"=====");
			string strFolder = path.Substring(0,path.LastIndexOf("\\"));
			
			if(!Directory.Exists(strFolder))
			{
				Directory.CreateDirectory(strFolder);
			}
			Delay.Milliseconds(1000);
			repo.选择要加载的文件.SelfInfo.WaitForExists(5000);
			repo.选择要加载的文件.Txt文件路径.Click();
			Delay.Milliseconds(1000);
			repo.选择要加载的文件.Txt文件路径.Element.SetAttributeValue("Text", path);
			repo.选择要加载的文件.Btn保存.Press();
			Delay.Milliseconds(4000);
		}
		#endregion
		
		#region 关闭资源管理器
		/// <summary>
		/// 时间：2018-6-26
		/// 作者：袁郑
		/// 功能：关闭资源管理器
		/// </summary>
		public static void CloseExplore()
		{
			Report.Info("=====关闭资源管理器=====");
			if (repo.Explore.SelfInfo.Exists())
			{
				repo.Explore.Self.Close();
			}
		}
		#endregion

	}
}
